/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.hotfixformetroapp.main;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seari.hotfixformetroapp.bean.StationConfig;
import static com.seari.hotfixformetroapp.main.HotfixGpsdataOfHistory.configPath;
import static com.seari.hotfixformetroapp.main.HotfixGpsdataOfHistory.properties;
import com.seari.hotfixformetroapp.service.StationConfigService;
import com.seari.hotfixformetroapp.utils.HttpClientUtil;
import com.seari.hotfixformetroapp.utils.MiscTools;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 *
 * @author Rainbow
 */
//@Component
//@Order(value = 2)
public class HotfixGpsdataOfStationConfig implements ApplicationRunner
{
    private List<StationConfig> stationConfigList;
    private Map<String,StationConfig> stationConfigMap; //站名与config对象对应的键值对
    private String baseUrl  = "https://restapi.amap.com/v3/place/text?key=2e22595df827fa4c8ff805f59f6e2ee4&city=shanghai&citylimit=true&types=150500&types=150600&keywords=";
    @Autowired
    private StationConfigService stationConfigService;
    
    @Override
    public void run(ApplicationArguments args) throws Exception
    {
        init();
        if(!CollectionUtils.isEmpty(stationConfigList))
        {
            //config对象转换为站名对应的键值对
            stationConfigMap = stationConfigList.stream().collect(Collectors.toMap(StationConfig::getName, Function.identity(),(key1,key2) -> key2)); //键冲突时取后来键
        }
        for (Map.Entry<String, StationConfig> entry : stationConfigMap.entrySet())
        {
            String key = entry.getKey();
            StationConfig stationConfig = entry.getValue();
            try
            {
                String response = HttpClientUtil.getInstance().sendHttpsGet(baseUrl+key);
                if(!StringUtils.isEmpty(response))
                {
                    JSONObject json = JSONObject.parseObject(response);
                    //System.out.println(json.toString());
                    JSONArray jsonArray = json.getJSONArray("pois");
                    if(jsonArray != null && !jsonArray.isEmpty())
                    {
                        JSONObject arrayElement =(JSONObject) jsonArray.get(0);
                        String locationStr = arrayElement.getString("location");
                        String[] axis = locationStr.split(",");
                        stationConfig.setGcjXaxis(axis[0]);
                        stationConfig.setGcjYaxis(axis[1]);
                        stationConfig.setType("jqdata");
                        stationConfigService.insertJQDataToStationConfig(stationConfig);
                        System.out.println("process data"+key);
                    }
                }else{
                    System.out.println("errdata:"+key);
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    
    private void init()
    {
        try
        {
            stationConfigList = stationConfigService.getDiffStationConfig();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
